<?php

//会员控制器
class Accounts extends Controller {

	var $viewpath;
	var $is_logined;
	var $prepage = 20;
	
	function Accounts()
	{
		parent::Controller();
		$this->viewpath = 'admin/'.__CLASS__;
		$this->load->model('managers_model','mm');
		$this->load->model('accounts_model','am');
		$this->load->model("site_model","sm");
		$this->is_logined = $this->mm->is_logined();
	}
	
	//默认首页
	function index()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		
		//搜索字符处理
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		$keyword = "";
		if (isset($this->param["keyword"])) {
			$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		}
		//获取列表
		(!isset($this->param["page"]) || $this->param["page"] == "") ? $offset = 0 : $offset = $this->param["page"];
		$items = $this->am->listitem($offset, $this->prepage, "id", "desc", $keyword);
		
		//获取会员身份
		foreach ($items as $key => $item) {
			$hasite = "非站长";
			$id = $item['id'];
			$site = $this->sm->get_usersite($id);
			if (is_array($site) && count($site) > 0) {
				$hasite = $site['name'];
			}
			$items[$key]['hasite'] = $hasite;
		}
		//分页数据处理
		$this->load->library('pagination');
		$base_url = site_url($this->config->item('admin_path')."/accounts/index/?keyword=".$keyword."&dosubmit=1");
		$total_rows = $this->am->countitems($keyword);//+keyword
		$pagestring = pages($base_url,$total_rows,3,$this->prepage, true);//query string
	
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$viewdata['keyword'] = $keyword;
		$viewdata['total_rows'] = $total_rows;
		$this->load->view($this->viewpath.'/index', $viewdata);
	}
	
	//查看
	function show()
	{
		$userid = u(4);
		//获取管理员信息
		$row = $this->am->get($userid);
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['row'] = $row;
		$this->load->view($this->viewpath.'/show', $viewdata);
	}
	
	//选择删除
	function delselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/accounts/index");
		//删除会员
		$this->am->delselected($delstr);
		//获取会员小站信息
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$site = $this->sm->get_usersite($_delid[$i]);
				if (is_array($site) && count($site) > 0) {
					$siteid = $site["id"];
					$this->sm->delete($siteid);
				}
				//删除粉丝!
				// ...
			}
		}
		admin_show_success("删除成功", $this->config->item('admin_path')."/accounts/index");	
	}
	
	//选择屏蔽
	function statusselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/accounts/index");
		//屏蔽会员
		$this->am->statusselected($delstr);
		admin_show_success("屏蔽成功", $this->config->item('admin_path')."/accounts/index");	
	}
	
	//单个删除
	function delete()
	{
		if(!$id = u(4)) redirect($this->config->item('admin_path')."/accounts/index");
		//删除会员
		$this->am->delete($id);
		//获取会员小站信息
		$site = $this->sm->get_usersite($id);
		if (is_array($site) && count($site) > 0) {
			$siteid = $site["id"];
			$this->sm->delete($siteid);
		}
		//删除粉丝!
		// ...
		admin_show_success("删除成功", $this->config->item('admin_path')."/accounts/index");	
	}
	
	//编辑
	function edit()
	{
		if($dosubmit = p("dosubmit")) {
			$userid = p('id');
			$result = $this->am->edit($userid);
			if($result) {//提示成功
				admin_show_success("编辑成功", $this->config->item('admin_path')."/accounts/");
			} else {
				admin_show_failed("编辑失败");
			}
		}
		if(!$userid = u(4)) {
			admin_show_failed("非法请求");
		}
		//获取信息
		$row = $this->am->get($userid);
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['row'] = $row;
		$this->load->view($this->viewpath.'/edit', $viewdata);
	}
}

/* End of file */